package com.wwz.tcc;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wwz.dao.OrderDao;
import com.wwz.feign.ProductFeign;
import com.wwz.feign.UserFeign;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.HashMap;
import java.util.List;

@Service
public class TccOrderServiceImpl implements TccOrderService{
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private UserFeign userFeign;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Override
    public Integer insert(BusinessActionContext businessActionContext,String name,long branchId) {

        int j =  orderDao.insert("5",System.currentTimeMillis(),5);

        return j;

    }

    @Override
    public Integer batchInsert(BusinessActionContext businessActionContext, String list,long branchId) {
        return orderDao.batchInsert(JSONArray.parseArray(list,JSONObject.class),branchId);
    }

    @Override
    public boolean commit(BusinessActionContext businessActionContext) {
        logger.info("==============ordersubmit===================");
        return true;
    }

    @Override
    public boolean rollback(BusinessActionContext businessActionContext) {
        logger.info("==============order-rollback===================");
        try{
            List<JSONObject> list = JSONArray.parseArray(businessActionContext.getActionContext("list").toString(),JSONObject.class);
            int i =  orderDao.batchDelete(list);
            return true;
        }catch (Exception e){
            return false;
        }
    }

}
